package com.example.lotterysystem.service.activitystatus.operater;

import com.example.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    /**
     * 判断奖品状态是否需要转换
     * 
     * @param convertActivityStausDTO 转换活动状态数据传输对象，包含奖品ID、活动ID和目标奖品状态
     * @return Boolean 返回是否需要转换奖品状态，true表示需要转换，false表示不需要
     */
    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStausDTO) {
        //判断奖品是否全部被抽完
        Long prizeId = convertActivityStausDTO.getPrizeId();
        Long activityId = convertActivityStausDTO.getActivityId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStausDTO.getTargetPrizeStatus();
        //奖品id，活动id，奖品状态
        if(null == activityId
                || null == targetPrizeStatus
                || null == prizeId){
            return false;
        }
        
        // 查询奖品信息
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId, prizeId);
        // 判断奖品信息是否存在
        if(null == activityPrizeDO){
            return false;
        }
        
        // 判断奖品状态是否一致
        if(targetPrizeStatus.name().equalsIgnoreCase(
                activityPrizeDO.getStatus())){
            return false;
        }

        return true;
    }

    /**
     * 转换奖品活动状态
     * @param convertActivityStatusDTO 状态转换参数
     * @return Boolean 操作结果，成功返回true，失败返回false
     */
    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        try {
            activityPrizeMapper.updateStatus(activityId, prizeId, targetPrizeStatus.name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }


}
